//
//  CoolVideoViewController.m
//  RunningMan
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 JohnnyB. All rights reserved.
//

#import "CoolVideoViewController.h"
#import "Header.h"
#import "ModelOfItemList.h"
#import "CellOfVideo.h"
#import "PlayVideoViewController.h"
#import "PlayingVideoViewController.h"
#import "JoPrivatismViewController.h"
#import "EMSDK.h"
#import "JoLoginViewController.h"
#import "ViewController.h"

@interface CoolVideoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrOfVideo;
@property (nonatomic, strong) UITableView *videoTableView;
@end

@implementation CoolVideoViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"酷玩";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"12"] style:UIBarButtonItemStylePlain target:self action:@selector(handlePushUserPage:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"y"] style:UIBarButtonItemStylePlain target:self action:@selector(handleGame:)];
    [self handleData];
    [self creatTableview];
    [self MJRefresh];
}

- (void)handleGame:(UIBarButtonItem *)button
{
    ViewController *viewController = [[ViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)handlePushUserPage:(UIBarButtonItem *)button
{
    BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;
    if (isAutoLogin) {
        JoPrivatismViewController *userPage = [[JoPrivatismViewController alloc]init];
        [self.navigationController pushViewController:userPage animated:YES];
    }
    else
    {
        JoLoginViewController *loginPage = [[JoLoginViewController alloc]init];
        [self presentViewController:loginPage animated:YES completion:^{
            
        }];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
}
-(void)MJRefresh{
    
    self.videoTableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
    
    self.videoTableView.mj_footer = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(handleDownLoadNewData)];
    
}

-(void)handleLoadNewData{
    
    [self handleData];
    [self.videoTableView.mj_header endRefreshing];
}
-(void)handleDownLoadNewData{
    
    
}


#pragma  mark - handleData
-(void)handleData{
    
//    [MBProgressHUD showMessage:@"小主莫急，努力加载中" toView:self.view];
    [APPTools GETWithURL:@"http://baobab.wandoujia.com/api/v3/videos?categoryId=18&num=20&start=0&strategy=date "par:nil success:^(id responseObject)
    {
        
        self.arrOfVideo = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"itemList"]) {
            ModelOfItemList *model = [[ModelOfItemList alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrOfVideo addObject:model];
        }
        
        [self.videoTableView reloadData];
        
        
        
    } filed:^(NSError *error) {
        //        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        //        [MBProgressHUD showError:@"哎呀，有很多调皮的小精灵干扰了网络呀"];
        NSLog(@"%@", error);
        
    }];
    
}

#pragma mark - TableView
-(void)creatTableview{
    
    self.videoTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    
    self.videoTableView.delegate = self;
    self.videoTableView.dataSource = self;
    self.videoTableView.showsVerticalScrollIndicator = NO;
    
    [self.videoTableView registerClass:[CellOfVideo class] forCellReuseIdentifier:@"CellOfVideoIdentifier"];
    self.videoTableView.separatorStyle = UITableViewCellEditingStyleNone;
    [self.view addSubview:self.videoTableView];
  
    
}

#pragma mark - TableView Data

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrOfVideo.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellOfVideo *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfVideoIdentifier"];
    ModelOfItemList *model = self.arrOfVideo[indexPath.row];
    cell.model = model;
    
    return cell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return SCREEN_HEIGHT/3;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PlayVideoViewController *play = [[PlayVideoViewController alloc] init];
    CATransition *animation = [CATransition animation];
    animation.duration = 0.5f;
    [animation setType:@"suckEffect"];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    [self.navigationController pushViewController:play animated:YES];
    
    ModelOfItemList *modelOfItemList = self.arrOfVideo[indexPath.row];
    play.modelOfDate = modelOfItemList.modelOfDate;
    
    
    
    
}

#pragma mark - 
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
